package com.jadefashion.controller;

import com.jadefashion.health.entity.QueryPageBean;
import com.jadefashion.health.entity.Result;
import com.jadefashion.health.resources.MessageConstant;
import com.jadefashion.pojo.Menu;
import com.jadefashion.service.MenuService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("menu")
public class MenuController {

    @Reference
    private MenuService menuService;

    /**
     * 根据用户姓名查询对用菜单信息
     *
     * @param map
     * @return
     */
    @RequestMapping("findTitle")
    public Result findTitle(@RequestBody Map map) {

        try {
            List<Menu> menus = menuService.findTitle(map);
            return new Result(true, MessageConstant.GET_MENU_SUCCESS, menus);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.GET_MENU_FAIL);
        }
    }

    /**
     * 查询菜单数据
     *
     * @param queryPageBean
     * @return
     */
    @RequestMapping("findPage")
    public Result findPage(@RequestBody QueryPageBean queryPageBean) {
        try {
            Map map = menuService.findPage(queryPageBean);
            return new Result(true, MessageConstant.QUERY_MENU_SUCCESS, map);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_MENU_FAIL);
        }
    }

    /**
     * 添加菜单信息
     *
     * @param menu
     * @return
     */
    @RequestMapping("handleAdd")
    public Result handleAdd(@RequestBody Menu menu) {
        try {
            menuService.handleAdd(menu);
            return new Result(true, MessageConstant.ADD_MENU_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_MENU_FAIL);
        }
    }

    /**
     * 修改菜单数据
     *
     * @param menu
     * @return
     */
    @RequestMapping("update")
    public Result update(@RequestBody Menu menu) {
        try {
            menuService.update(menu);
            return new Result(true, MessageConstant.EDIT_MENU_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.EDIT_MENU_FAIL);
        }
    }

    /**
     * 删除菜单数据
     *
     * @param id
     * @return
     */
    @RequestMapping("delete")
    public Result delete(@RequestParam("id") Integer id) {
        try {
            menuService.delete(id);
            return new Result(true, MessageConstant.DELETE_MENU_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.DELETE_MENU_FAIL);
        }
    }

    /**
     * 查询所有菜单信息
     *
     * @return
     */
    @RequestMapping("findAll")
    public Result findAll() {
        try {
            List<Menu> list = menuService.findAll();
            return new Result(true, MessageConstant.QUERY_MENU_SUCCESS, list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_MENU_FAIL);
        }
    }

    /**
     * 查询角色对应的菜单
     */
    @RequestMapping("queryMenuForRole")
    public Result queryMenuForRole(@RequestBody Map map) {

        try {
            //获取前端传递的角色id
            Integer id = (Integer) map.get("id");
            List<Menu> list = menuService.findByRolerId(id);
            return new Result(true, MessageConstant.QUERY_MENU_SUCCESS, list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_MENU_FAIL);
        }
    }
}
